package com.adcc.ags.gms.core.util;

import com.adcc.utility.mq.configuration.MQConfiguration;
import com.adcc.utility.mq.manager.ibm.IBMMQManager;
import com.adcc.utility.mq.transfer.MQConnectionPoolFactory;
import com.adcc.utility.mq.transfer.ibm.IBMMQConnectionPool;
import com.adcc.utility.mq.transfer.ibm.IBMMQTransfer;
import org.springframework.stereotype.Component;

import java.util.Hashtable;

/**
 * MQ工厂类
 */
@Component
public class MQFactory {

    // Source MQManager
    private IBMMQManager sourceMQM;

    // Source MQTransfer
    private IBMMQTransfer sourceMQT;

    // Target MQManager
    private IBMMQManager targetMQM;

    // Target MQTransfer
    private IBMMQTransfer targetMQT;

    /**
     * 构造函数
     */
    public MQFactory(){

    }

    /**
     * 构造函数
     * @param configuration
     * @throws Exception
     */
    public MQFactory(MQConfiguration configuration) throws Exception {
        IBMMQConnectionPool pool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(configuration);
        sourceMQM = new IBMMQManager(configuration,pool);
        sourceMQT = new IBMMQTransfer(configuration,pool);
    }

    /**
     * 构造函数
     * @param configuration
     * @param params
     * @throws Exception
     */
    public MQFactory(MQConfiguration configuration,Hashtable<String,Object> params) throws Exception {
        IBMMQConnectionPool pool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(configuration);
        pool.setActiveMode(Integer.valueOf(params.get("ActiveMode").toString()));
        pool.setTimeout(Long.valueOf(params.get("Timeout").toString()));
        pool.setMaxConnections(Integer.valueOf(params.get("MaxConnections").toString()));
        pool.setMaxIdelConnections(Integer.valueOf(params.get("MaxIdelConnections").toString()));
        sourceMQM = new IBMMQManager(configuration,pool);
        sourceMQT = new IBMMQTransfer(configuration,pool);
    }

    /**
     * 构造函数
     * @param source
     * @param target
     * @throws Exception
     */
    public MQFactory(MQConfiguration source,MQConfiguration target) throws Exception {
        IBMMQConnectionPool sourcePool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(source);
        sourceMQM = new IBMMQManager(source,sourcePool);
        sourceMQT = new IBMMQTransfer(source,sourcePool);
        IBMMQConnectionPool targetPool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(target);
        targetMQM = new IBMMQManager(target,targetPool);
        targetMQT = new IBMMQTransfer(target,targetPool);

    }

    /**
     * 构造函数
     * @param source
     * @param target
     * @throws Exception
     */
    public MQFactory(MQConfiguration source,Hashtable<String,Object> params1,MQConfiguration target,Hashtable<String,Object> params2) throws Exception {
        IBMMQConnectionPool sourcePool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(source);
        sourcePool.setActiveMode(Integer.valueOf(params1.get("ActiveMode").toString()));
        sourcePool.setTimeout(Long.valueOf(params1.get("Timeout").toString()));
        sourcePool.setMaxConnections(Integer.valueOf(params1.get("MaxConnections").toString()));
        sourcePool.setMaxIdelConnections(Integer.valueOf(params1.get("MaxIdelConnections").toString()));
        sourceMQM = new IBMMQManager(source,sourcePool);
        sourceMQT = new IBMMQTransfer(source,sourcePool);
        IBMMQConnectionPool targetPool = (IBMMQConnectionPool)MQConnectionPoolFactory.getInstance().createIBMMQConnectionPool(target);
        targetPool.setActiveMode(Integer.valueOf(params2.get("ActiveMode").toString()));
        targetPool.setTimeout(Long.valueOf(params2.get("Timeout").toString()));
        targetPool.setMaxConnections(Integer.valueOf(params2.get("MaxConnections").toString()));
        targetPool.setMaxIdelConnections(Integer.valueOf(params2.get("MaxIdelConnections").toString()));
        targetMQM = new IBMMQManager(target,targetPool);
        targetMQT = new IBMMQTransfer(target,targetPool);
    }

    public IBMMQManager getSourceMQM() {
        return sourceMQM;
    }

    public IBMMQTransfer getSourceMQT() {
        return sourceMQT;
    }

    public IBMMQManager getTargetMQM() {
        return targetMQM;
    }

    public IBMMQTransfer getTargetMQT() {
        return targetMQT;
    }
}
